package com.redsoft.spirit.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.redsoft.spirit.entity.JsonResult;
import com.redsoft.spirit.entity.Menu;
import com.redsoft.spirit.entity.TreeNode;
import com.redsoft.spirit.service.MenuService;

@RestController
public class MenuController {

	@Autowired
	private MenuService menuService;

	private Gson gson = new Gson();

	@RequestMapping("/menus/getJsonTree/{userId}")
	public String getJsonTree(@PathVariable String userId) {

		try {
			List<TreeNode> menu = menuService.getJsonTree(userId);
			JsonResult result = new JsonResult(menu);
			return result.success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 查询菜单
	 * 
	 * <pre>
	 * 根据传过来的id查询该菜单以及该菜单的儿子们菜单
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 *            菜单id
	 * @return
	 */
	@RequestMapping(value = "/menus", method = RequestMethod.GET)
	public String getMenu(String id) {
		try {
			Menu menu = menuService.get(id);
			List<Menu> children = menuService.getChildrenMenu(id);

			Map map = new HashMap<>();
			map.put("menu", menu);
			map.put("children", children);

			return new JsonResult(map).success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 修改菜单
	 * 
	 * <pre>
	 * 修改菜单的基本信息
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param menu
	 *            菜单实体类
	 * @return
	 */
	@RequestMapping(value = "/menus", method = RequestMethod.PUT)
	public String updateMenu(@RequestBody Menu menu) {

		try {
			menuService.updateMenu(menu);
			return new JsonResult().success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 增加菜单
	 * 
	 * <pre>
	 * 增加菜单的基本信息
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param menu
	 *            菜单实体类
	 * @return
	 */
	@RequestMapping(value = "/menus", method = RequestMethod.POST)
	public String addMenu(@RequestBody Menu menu) {
		try {
			menuService.addMenu(menu);
			return new JsonResult().success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 删除菜单
	 * 
	 * <pre>
	 * 删除菜单的同时需要删除菜单和角色的关系
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/menus", method = RequestMethod.DELETE)
	public String deleteMenu(String id) {
		try {
			menuService.deleteMenu(id);
			return new JsonResult().success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 增加角色和菜单的关系
	 * 
	 * <pre>
	 * 在这个菜单下保存分配的角色
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@RequestMapping(value = "/menus/{menuId}/roles", method = RequestMethod.POST)
	public String addMenuRole(@PathVariable String menuId, String roleIds) {
		try {
			String[] roles = roleIds.split(",");
			List<String> list = java.util.Arrays.asList(roles);

			menuService.addMenuRole(menuId, list);
			return new JsonResult().success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 
	 * 根据菜单查询拥有该菜单的角色.
	 * 
	 * <pre>
	 * 根据菜单查询拥有该菜单的角色
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param menuId
	 * @return
	 */
	@RequestMapping(value = "/menus/{menuId}/roles", method = RequestMethod.GET)
	public String getRoleByMenu(@PathVariable String menuId) {
		try {

			List<String> list = menuService.getRoleByMenu(menuId);

			return new JsonResult(list).success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());

			return result.failure().toString();
		}
	}

	/**
	 * 
	 * 根据人员得到这个人员拥有的所有菜单.
	 * 
	 * <pre>
	 * 根据人员得到这个人员拥有的所有菜单
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@RequestMapping(value = "/menus/users/{userId}", method = RequestMethod.GET)
	public String getMenusByUser(@PathVariable String userId) {

		try {
			List<TreeNode> dept = menuService.getJsonTreeByUser(userId);
			JsonResult result = new JsonResult(dept);

			return result.success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());

			return result.failure().toString();
		}
	}

	/**
	 * 
	 * 根据人员id和角色id得到这个人员拥有的所有菜单以及该角色拥有的所有菜单.
	 * 
	 * <pre>
	 * 根据人员id和角色id得到这个人员拥有的所有菜单以及该角色拥有的所有菜单
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@RequestMapping(value = "/menus/users/{userId}/roles/{roleId}", method = RequestMethod.GET)
	public String getMenusByUserRole(@PathVariable String userId, @PathVariable String roleId) {

		List<TreeNode> tree = menuService.getAllJsonTreeByUser(userId);
		List<Menu> checksList = menuService.getMenuIdsByRole(roleId);
		String[] treeChecks = null;
		if (checksList.size() > 0) {
			StringBuilder str = new StringBuilder();
			for (Menu item: checksList) {
				str.append(item.getId())
				.append(",");
			}
			str.deleteCharAt(str.length() - 1);
			
			treeChecks = str.toString().split(",");
		}

		Map<String, Object> map = new HashMap<>();
		map.put("treeJson", tree);
		map.put("treeChecks", treeChecks);

		JsonResult result = new JsonResult(map);

		return result.success().toString();
	}
}